package com.hyy.cmppserver.util;

import cn.hutool.core.collection.CollUtil;
import com.hyy.api.domain.customer.CstmCustomer;
import com.hyy.api.domain.customer.redis.CstmCustomerCmppRedisDto;
import com.hyy.common.redis.utils.RedisUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.hyy.api.constant.BusinessRedisConstants.CSTM_CUSTOMER_CMPP_CACHE;

@Service
public class CacheService {

    public List<CstmCustomerCmppRedisDto> getAllValidCustomer() {
        Collection<String> keys = RedisUtils.keys(CSTM_CUSTOMER_CMPP_CACHE.replace("%s", "*"));
        List<CstmCustomerCmppRedisDto> cstmCustomerCmppRedisDtos = new ArrayList<>();
        for (String key : keys) {
            CstmCustomerCmppRedisDto cstmCustomerCmppRedisDto = RedisUtils.getCacheObject(key);
            if (cstmCustomerCmppRedisDto != null) {
                cstmCustomerCmppRedisDtos.add(cstmCustomerCmppRedisDto);
            }
        }

        return cstmCustomerCmppRedisDtos;
    }

    public void updateConnections(Long id, String s) {
    }

    public CstmCustomerCmppRedisDto getCustomer(String userName) {
        String redisKey = String.format(CSTM_CUSTOMER_CMPP_CACHE, userName);
        return  RedisUtils.getCacheObject(redisKey);
    }
}
